import { ENUM_CLIP, ENUM_UI } from "../GbzEnum";
import GbzInstance from "../GbzInstance";
import { timerFormat } from "../GbzUtils";
import GbzAudioManager from "../manager/GbzAudioManager";
import GbzDataManager from "../manager/GbzDataManager";
import GbzSdkManager from "../manager/GbzSdkManager";
import BaseLayer from "./GbzBaselayer";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GbzWinLayer extends BaseLayer {

    panel: cc.Node = null
    btn_next: cc.Node = null
    btn_share: cc.Node = null
    tip_label: cc.Label = null

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btn_next = cc.find('buttons/btn_next', this.panel)
        this.btn_next.on('click', this.onNext, this)
        this.btn_share = cc.find('buttons/btn_share', this.panel)
        this.btn_share.on('click', this.onShare, this)
        const tip = cc.find('subtip', this.panel)
        this.tip_label = tip.getComponent(cc.Label)
    }

    onDestroy() {
        this.btn_next.off('click', this.onNext, this)
        this.btn_share.off('click', this.onShare, this)
    }

    onEnable() {
        this.zoom(this.panel)
        GbzSdkManager.ins.showBanner(true)
        this.rendor()
    }

    onDisable() {
        GbzSdkManager.ins.showBanner(false)
    }

    rendor() {
        let score = 20
        this.tip_label.getComponent(cc.Label).string = `游戏耗时${timerFormat(GbzDataManager.ins.data_time_count, 'i:s')}，排行榜得分+${score}`
    }

    async onNext() {
        GbzAudioManager.ins.play(ENUM_CLIP.GAME_CLICK)
        await GbzInstance.fadeMgr.in()
        GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_WIN, false)
        GbzInstance.gameMgr.onGameStart()
    }

    onShare() {
        GbzAudioManager.ins.play(ENUM_CLIP.GAME_CLICK)
        if (GbzSdkManager.ins.platform()) GbzSdkManager.ins.activeShare()
    }
}
